<?php
/**
 * 门店员工
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;

use Model\StorePositionMod;
use Model\StoreStaffMod;
use Pub\cache\Cache;
use Pub\iarray\IArray;

class StoreStaff extends Action
{
    private $StoreStaff = null;   //门店职员
    private $StorePosition = null;  //门店职位

    public function __construct()
    {
        parent::__construct();
        $this->StoreStaff = new StoreStaffMod();
    }


    //S-----前端门店管理------

    /**
     * 获取转让二维码
     * @param soter_id  门店id
     */
    public function getTransferQrcode()
    {
        $_dir = 'transfer_qrcode';
        $_filename = $this->_gpc['store_id'];
        $_url = murl('entry//iscan', array('m' => 'kuyuan_vbz', 'type' => 'transfer', 'store_id' => $this->_gpc['store_id']), true, true);
        $this->_result = array('status' => 'success', 'data' => $this->getQrcode($_dir, $_filename, $_url,true));
        $this->result($this->_result);
    }

    /**
     * 获取转让的用户信息
     * @param store_id 门店id
     */
    public function getTransferUser()
    {
        $_store_id = $this->_gpc['store_id'];
        $_transfer_user = Cache::get(Cache::STORE_TRANSFER,array('store_id'=>$_store_id));
//        $_transfer_user = array(
//            'user'=>array(),
//            'past_time'=>TIMESTAMP+60*5
//        );
        if ($_transfer_user && $_transfer_user['past_time'] > TIMESTAMP) {
            $this->_result = array('status' => 'success', 'data' => $_transfer_user['user']);
        }
        $this->result($this->_result);
    }

    /**
     * 删除转让门店申请
     * @param store_id 门店id
     */
    public function deleteTransfer()
    {
        $_store_id = $this->_gpc['store_id'];
        Cache::remove(Cache::STORE_TRANSFER,array('store_id'=>$_store_id));
        $this->_result = array('status'=>'success');
        $this->result($this->_result);
    }

    /**
     * 转让门店给指定用户
     * @param uid  转让的目标用户id
     * @param store_id 门店id
     */
    public function confirmTransfer()
    {
        if($this->StoreStaff->update(array('uid'=>$this->_gpc['uid']),array('store_id'=>$this->_gpc['store_id'],'position_id'=>0))){
            Cache::remove(Cache::STORE_TRANSFER,array('store_id'=>$this->_gpc['store_id']));
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 获取申请员工二维码地址
     * @param store_id  门店id
     */
     public function getAddQrcode()
     {
         $_dir = 'staff_qrcode';
         $_filename = $this->_gpc['store_id'];
         $_url = murl('entry//iscan', array('m' => 'kuyuan_vbz', 'type' => 'staff', 'store_id' => $this->_gpc['store_id']), true, true);
         $this->_result = array('status' => 'success', 'data' => $this->getQrcode($_dir, $_filename, $_url,true));
         $this->result($this->_result);
     }

    /**
     * 获取申请员工列表
     * @param store_id 门店id
     */
     public function getApplyList()
     {
         $_store_id = $this->_gpc['store_id'];
         $_data = Cache::get(Cache::STORE_STAFF_APPLY,array('store_id'=>$_store_id));
         if($_data){
             $_data = IArray::shiftPastTime($_data);
             if(count($_data) > 0){
                $this->_result = array('status'=>'success','data'=>$_data);
             }
         }
         $this->result($this->_result);
     }

    /**
     * 设置门店员工
     * @param uid 用户id
     * @param position_id  职位id
     * @param store_id 门店id
     */
     public function setStaff()
     {
        if($this->StoreStaff->findOne(array('uid'=>$this->_gpc['data']['uid'],'uniacid'=>$this->_uniacid),array('id'))){
            $this->_result = array('status'=>'error','err_msg'=>'当前用户已是某门店的员工了！','err_status'=>9);
        }else{
            $this->_gpc['data']['uniacid'] = $this->_uniacid;
            if($this->StoreStaff->insert($this->_gpc['data'])){
                $_apply_list = Cache::get(Cache::STORE_STAFF_APPLY,array('store_id'=>$this->_gpc['data']['store_id']));
                $_apply_list = IArray::shift($_apply_list,$this->_gpc['data']['uid']);
                Cache::set(Cache::STORE_STAFF_APPLY,$_apply_list,array('store_id'=>$this->_gpc['data']['store_id']));
                $this->_result = array('status'=>'success');
            }else{
                $this->_result = array('status'=>'error','err_msg'=>'设置失败','err_status'=>8);
            }
        }
        $this->result($this->_result);
     }

    /**
     * 修改指定用户的职位
     * @param position_id 职位id
     * @param staff_id 职员id
     */
     public function changeStaff()
     {
        if($this->StoreStaff->update(array('position_id'=>$this->_gpc['position_id']),array('id'=>$this->_gpc['staff_id']))){
            $this->_result = array('status'=>'success');
        }else{
            $this->_result = array('status'=>'error','err_msg'=>'修改失败','err_status'=>9);
        }
        $this->result($this->_result);
     }

    /**
     * 员工列表
     * @param store_id
     */
     public function li()
     {
         $_list = $this->StoreStaff->findAll(array('store_id'=>$this->_gpc['store_id'],'position_id !='=>0),array('order'=>array('id','desc')),array('id','uid','position_id'));
         if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                if($_v['uid'] == $this->_gpc['uid']){
                    unset($_list[$_k]);
                    continue;
                }
                $_member = $this->uidToInfo($_v['uid']);
                $_list[$_k]['nickname'] = $_member['nickname'];
                $_list[$_k]['header'] = $_member['avatar'];
                $_list[$_k]['position'] = $this->positionIdToNmae($_v['position_id']);
            }
            unset($_member);
            $this->_result = array('status'=>'success','data'=>$_list);
         }
         $this->result($this->_result);
     }

    /**
     * 删除门店员工
     * @param staff_id 员工id
     */
     public function delete()
     {
        if($this->StoreStaff->delete(array('id'=>$this->_gpc['staff_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
     }
    //E-----前端门店管理------




}